<!DOCTYPE html>
<html lang="en">
  
  <head>
    <meta charset="utf-8">
    <!-- YAHOO JS -->
    <script type="text/javascript" src="js/yahoo.js">
    </script>
    <script type="text/javascript" src="js/dom.js">
    </script>
    <script type="text/javascript" src="js/event.js">
    </script>
    <script type="text/javascript" src="js/connection.js">
    </script>
    <!-- UNMODIFIED IMO JS -->
    <script type="text/javascript" src="js/imo.js">
    </script>
    <script type="text/javascript" src="js/ui.js">
    </script>
    <script type="text/javascript" src="js/i18n.js">
    </script>
    <script type="text/javascript" src="js/constants.js">
    </script>
    <script type="text/javascript" src="js/binheap.js">
    </script>
    <script type="text/javascript" src="js/dispatcher.js">
    </script>
    <script type="text/javascript" src="js/network.js">
    </script>
    <script type="text/javascript" src="js/utils.js">
    </script>
    <script type="text/javascript" src="js/utils2.js">
    </script>
    <script type="text/javascript" src="js/mersenne-twister.js">
    </script>
    <!-- CHROME EXT JS -->
    <script type="text/javascript" src="js/sessionManager.js">
    </script>
    <script type="text/javascript" src="js/new_ajax.js">
    </script>
    <script type="text/javascript" src="js/tabManager.js">
    </script>
    <script type="text/javascript" src="js/commManager.js">
    </script>
    <script type="text/javascript" src="js/convManager.js">
    </script>
    <script type="text/javascript" src="js/prefManager.js">
    </script>
    <script type="text/javascript" src="js/blistManager.js">
    </script>
    <script type="text/javascript" src="js/statusManager.js">
    </script>
    <script type="text/javascript" src="js/activityManager.js">
    </script>
    <script>
      IMO.Constants['IMOURL'] = "https://o.imo.im/";
      var MT = new MersenneTwister(); /* seed the MT random number generator */
      //chrome.windows.getCurrent(function(window) {
      //    var seed = (new Date().getTime())*window.id;
      //    seed /= window.width;
      //    MT = new MersenneTwister(Math.floor(seed));
      //});
      var ui = IMO.UI.X;
      ui._createManager(IMO.Manager.SessionManager);
      ui._createManager(IMO.Manager.TabManager);
      ui._createManager(IMO.Manager.CommManager);
      ui._createManager(IMO.Manager.ConvManager);
      ui._createManager(IMO.Manager.PrefManager);
      ui._createManager(IMO.Manager.BlistManager);
      ui._createManager(IMO.Manager.StatusManager);
      ui._createManager(IMO.Manager.ActivityManager);

      var tabManager = ui.getModule(IMO.Manager.TabManager);
      var convManager = ui.getModule(IMO.Manager.ConvManager);
      var commManager = ui.getModule(IMO.Manager.CommManager);
      var sessionManager = ui.getModule(IMO.Manager.SessionManager);
      var prefManager = ui.getModule(IMO.Manager.PrefManager);
      var blistManager = ui.getModule(IMO.Manager.BlistManager);
      var statusManager = ui.getModule(IMO.Manager.StatusManager);
      var activityManager = ui.getModule(IMO.Manager.ActivityManager);

      // otra globals
      var notificationTimeout = 3;
      var iconURL = chrome.extension.getURL("images/i_48x48.png");
      var audio = null;
      var enableAudioNotifications = false;
      var enableMessageNotifications = false;

      function randomString(len) {
        len = len ? len : 16;
        var c = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" + "0123456789";
        var str = [];
        for (var i = 0; i < len; i++) {
          var d = new Date();
          var index = Math.floor(MT.random() * c.length);
          str.push(c.substring(index, index + 1));
        }
        return str.join("");
      }

      function getPopupPage() {
        var views = chrome.extension.getViews();
        for (var i = 0; i < views.length; i++) {
          var view = views[i];
          if (view.location.href == chrome.extension.getURL('popup.html') || view.location.href == chrome.extension.getURL('popup.html#')) {
            return view;
          }
        }
        return null;
      }

      function loadSound() {
        audio = new Audio();
        audio.id = "beeeeep";
        audio.onerror = function() {
          console.log("sound not loaded: " + JSON.stringify(audio.error));
        }
        audio.addEventListener("canplaythrough", function() {
          console.log("sound loaded");
        }, false);
        //audio.src = "http://dl.google.com/dl/chrome/extensions/audio/click.mp3";
        audio.src = "https://imo.im/sounds/beep.mp3";
        audio.load();
      }

      function readConfig() {
        enableAudioNotifications = localStorage.enableAudioNotifications == "true";
        enableMessageNotifications = localStorage.enableMessageNotifications == "true";
        notificationTimeout = parseInt(localStorage.notificationTimeout, 10);
        if (isNaN(notificationTimeout)) {
          notificationTimeout = 3;
        }
        console.log('play sound: ' + enableAudioNotifications);
        console.log('display notifications: ' + enableMessageNotifications);
        console.log('notification timeout: ' + notificationTimeout);
      }

      function checkPermission() {
        try {
          if (localStorage.enableMessageNotifications == "true" && webkitNotifications.checkPermission() != 0) {
            // user thinks he's given permission, but we've lost it b'cos
            // of chrome update or something
            localStorage.enableMessageNotifications = false;
            chrome.tabs.create({
              url: 'options.html'
            });
          }
        }
        catch (e) {
          console.log('Error checking permission: ' + e.message);
        }
      }

      function initPermissions() {
        if (localStorage.firstRun == undefined) {
          chrome.tabs.create({
            url: 'options.html'
          });
          localStorage.firstRun = randomString(16);
          commManager.firstRun(localStorage.firstRun);
        }
        else {
          console.log('running instance: ' + localStorage.firstRun);
          checkPermission();
        }
      }

      function firePopupEvent(type, key, obj) {
        var popup = getPopupPage();
        if (popup) {
          console.log('firing ' + type);
          popup.gPopupEvent.fire(type, key, obj);
        }
        else {
          //console.log('popup not visible');
        }
      }

      function playAlertSound() {
        if (!enableAudioNotifications) {
          return;
        }

        if (audio.currentTime > 0) {
          audio.pause();
          audio.currentTime = 0;
        }
        audio.play();
      }

      function displayNotification(title, content) {
        if (!enableMessageNotifications || getPopupPage()) {
          return;
        }

        if (title && title.length > 50) {
          title = title.substring(0, 50) + '...';
        }

        if (content && content.length > 150) {
          content = content.substring(0, 150) + '...';
        }

        try {
          var notification = webkitNotifications.createNotification(iconURL, title, content);
          // TODO: When the user cancel's the notification explicitly, mark it as
          // read
          notification.show();
          window.setTimeout(function() {
            notification.cancel();
          }, notificationTimeout * 1000);
        }
        catch (e) {
          console.log('Error displaying notification: ' + e.message);
        }
      };

      loadSound();
      initPermissions();
      readConfig();
    </script>
  </head>
  
  <body>
  </body>

</html>